﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace EDMS
{
    public partial class UpdateVoter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initVoter();
            }
        }
        //判断身份证号是否重复
        private static bool checkSfzh(string sf)
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                Xm vo = dc.Xm.FirstOrDefault(v => v.sfzh == sf);
                if (vo != null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        //确定
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtXmxm.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入选民姓名！");
            }
            else if (txtSybwtcs.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请填写剩余委托次数！");
            }
            else if (cmbXb.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请选择性别！");
            }
            else
            {
                updateVoter();
            }
        }
        //初始化
        private void initVoter()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var vo = from v in dc.Xm
                         where v.sfzh == Request.QueryString["VID"]
                         select new
                         {
                             bh = v.xjbh,
                             xm = v.xmxm,
                             sfz = v.sfzh,
                             xb = v.xb,
                             sjh = v.sjhm,
                             bj = v.flag,
                             sycs = v.sybwtcs,
                             sr = v.sr,
                             zz = v.zz,
                             cym = v.cym
                         };
                foreach (var i in vo)
                {
                    cmbXjbh.Text = i.bh;
                    txtSfzh.Text = i.sfz;
                    txtXmxm.Text = i.xm;
                    txtCym.Text = i.cym;
                    txtSybwtcs.Text = i.sycs.ToString();
                    txtZz.Text = i.zz;
                    txtSjhm.Text = i.sjh;
                    cmbXb.Value = i.xb;
                    deSr.Value = i.sr.ToString();
                }

            };
        }

        //修改信息
        private void updateVoter()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var xm = dc.Xm.Select(x => x)
                   .Where(x => x.sfzh == Request.QueryString["VID"])
                   .SingleOrDefault();
                xm.xmxm = txtXmxm.Text.Trim().ToString();
                xm.sfzh = txtSfzh.Text.Trim().ToString();
                xm.sjhm = txtSjhm.Text.Trim().ToString();
                xm.sybwtcs = Convert.ToInt32(txtSybwtcs.Text);
                xm.xb = cmbXb.Value.ToString();
                xm.sr = deSr.Text.ToString();
                xm.zz = txtZz.Text;
                xm.cym = txtCym.Text;
                dc.SubmitChanges();
                Frame.Global.GlobalFunctions.Alert("选民信息修改成功！");
            };
        }

        protected void btnEsc_Click(object sender, EventArgs e)
        {
            Response.Redirect("VoterInfo.aspx");
        }
    }
}